describe("api_accounts", function() {
    var api, response;

    beforeEach(function() {
        App.initSession();
        response = {
            productListResponse: {
                loanList: [{
                    "currentBalance": 1887.72
                }],
                cardList: [{
                    "currentBalance": 1887.72
                }],
                responseHeader: {
                    status: 1,
                    currentTimestamp: "2011-10-01T13:58:33.104+0530",
                    nonce: "2175d8662534e58e9c77ca35def779f7"
                }
            }
        };

        api = App.api_accounts;

    });

    describe("_processProductList",function(){
        var callback = jasmine.createSpy();
        var t_productListReponse;
        beforeEach(function(){
            t_productListReponse ={"productListResponse":{"responseHeader":{"status":1,"nonce":"b8e5ef976a2d2b5ab632f5311f95b227","currentTimestamp":"2013-04-25T15:36:43.760+0800"},"productList":[{"accountName":"MASKED SHORT NAME CHANGED IN EBBS","accountNumber":"02105157135","accountType":"Personal Current Account-Rupiah","currencyCode":"IDR","productCategory":"CASA","currentBalance":-237048948.00,"availableBalance":451052,"lineAmount":237500000},{"accountName":"XXXX","accountNumber":"02113004164","accountType":"Access Plus Account","currencyCode":"IDR","productCategory":"CASA","currentBalance":313431.95,"availableBalance":313431.9500000000116415321826934814453125,"lineAmount":0.0},{"accountName":"XXXX","accountNumber":"02100014891","accountType":"Personal Current Account-Rupiah","currencyCode":"IDR","productCategory":"CASA","currentBalance":-446543008.23,"availableBalance":3455381.769999980926513671875,"lineAmount":450000000},{"accountName":"XXXX","accountNumber":"02105081619","accountType":"Personal Current Account-Rupiah","currencyCode":"IDR","productCategory":"CASA","currentBalance":-224147822.84,"availableBalance":852177.1599999964237213134765625,"lineAmount":225000000},{"accountName":"XXXX","accountNumber":"02111220900","accountType":"Customer DCI Mastersaving-Rupiah","currencyCode":"IDR","productCategory":"CASA","currentBalance":0.00,"availableBalance":0.0,"lineAmount":0.0},{"accountName":"XXXX","accountNumber":"02131012513","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":100000000.000,"availableBalance":100000000.000,"lineAmount":0},{"accountName":"XXXX","accountNumber":"02131012572","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":100000000.000,"availableBalance":100000000.000,"lineAmount":0},{"accountName":"XXXX","accountNumber":"02131020117","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":50000000.000,"availableBalance":50000000.000,"lineAmount":0},{"accountName":"XXXX","accountNumber":"02134008169","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":250000000.000,"availableBalance":250000000.000,"lineAmount":0},{"accountName":"XXXX","accountNumber":"31000007489","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":311475698.450,"availableBalance":311475698.450,"lineAmount":0},{"accountName":"XXXX","accountNumber":"31000024765","accountType":"Local Fix Deposit","currencyCode":"IDR","productCategory":"CASA","currentBalance":250000000.000,"availableBalance":250000000.000,"lineAmount":0}],"cardList":[{"accountNumber":"4934991551100012","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":0,"cardType":"Visa Infinite","cardStatus":"1","blockCode":"","statementBalance":0,"minimumAmountDue":0},{"accountNumber":"5149342010001003","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":0,"cardType":"Combo Card","cardStatus":"1","blockCode":"","statementBalance":0,"minimumAmountDue":0},{"accountNumber":"5149342010008859","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":1050000,"cardType":"Combo Card","cardStatus":"2","blockCode":"P","statementBalance":1050000,"minimumAmountDue":2050},{"accountNumber":"5149342080085555","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":-14257004.00,"cardType":"Titanium","cardStatus":"2","blockCode":"","statementBalance":-14257004.00,"minimumAmountDue":0},{"accountNumber":"5149342080196550","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":-9404520.0,"cardType":"Titanium","cardStatus":"1","blockCode":"","statementBalance":-9404520.0,"minimumAmountDue":0},{"accountNumber":"4511960004505086","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":-10002000,"cardType":"Visa Classic ","cardStatus":"2","blockCode":"","statementBalance":-10002000,"minimumAmountDue":0},{"accountNumber":"4511964900000010","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":0,"cardType":"Visa Classic Supplementary","cardStatus":"1","blockCode":"","statementBalance":0,"minimumAmountDue":0},{"accountNumber":"4511964900000028","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":0,"cardType":"Visa Classic Supplementary","cardStatus":"2","blockCode":"","statementBalance":0,"minimumAmountDue":0},{"accountNumber":"4511964900000036","accountType":"190","currencyCode":"IDR","productCategory":"CARD","currentBalance":0,"cardType":"Visa Classic Supplementary","cardStatus":"2","blockCode":"","statementBalance":0,"minimumAmountDue":0}],"loanList":[{"accountNumber":"604","accountType":"HOME EASY - STL","currencyCode":"IDR","currentBalance":0,"availableBalance":499881753},{"accountNumber":"744","accountType":"HOME INVEST-RB","currencyCode":"IDR","currentBalance":0,"availableBalance":52875000},{"accountNumber":"1333","accountType":"EDUCATION LOAN-STL","currencyCode":"IDR","currentBalance":0,"availableBalance":40000000}],"totalAccountBalance":154049351.330,"filtered":false}};
            App.api_accounts.callbackProductList = function() {
            };
        });

        it('error response', function() {
            spyOn(bsoi, 'validateResponse').andThrow(BreezeException.create());

            spyOn(App.api_accounts, 'callbackProductList');
            App.api_accounts._processProductList({});

            expect(App.api_accounts.callbackProductList).toHaveBeenCalledWith(jasmine.any(BreezeException));
        });

        it('User can see ProductList', function() {
            spyOn(bsoi, 'validateResponse');

            spyOn(App.api_accounts, '_buildAccountList').andCallFake(function(products, accountType) {
                if (accountType == 'product') {
                    return {
                        totalBalance: 1000,
                        list: [{}]
                    }
                }
                if (accountType == 'card') {
                    return {
                        totalBalance: 2000,
                        list: [{}, {}]
                    }
                }
                if (accountType == 'loan') {
                    return {
                        totalBalance: 3000,
                        list: [{}, {}, {}]
                    }
                }
            });

            spyOn(App.getSession(), 'setProductList');
            spyOn(App.api_accounts, 'callbackProductList');

            App.api_accounts._processProductList({productListResponse: {totalAccountBalance: 20000}});

            expect(App.getSession().setProductList).toHaveBeenCalledWith({
                iHave: {
                    totalBalance: 20000,
                    list: [{}]
                },
                iOwe: {
                    totalBalance: 5000,
                    list: [{}, {}, {}, {}, {}]
                }
            });

            expect(App.api_accounts.callbackProductList).toHaveBeenCalledWith(null);

        });

    });

    describe("buildAccountList",function(){
        it("accountType is loan", function() {

            var accountListObj = api._buildAccountList(response.productListResponse.loanList, 'loan');

            expect(accountListObj.totalBalance).toEqual('1887.72');
            expect(accountListObj.list[0] instanceof App.LoanAccount).toBeTruthy();
        });

        it("accountType is card", function() {
            var accountListObj = api._buildAccountList(response.productListResponse.cardList, 'card');

            expect(accountListObj.totalBalance).toEqual('1887.72');
            expect(accountListObj.list[0] instanceof App.CardAccount).toBeTruthy();
        });

        it("accountType is product", function() {
//            var productList = [{
//                localCurrentBalance: 100.12,
//                productCode: "DDA",
//                subProductCode: "100",
//                productGroup: "STMT"
//            }];
//            var t_account = App.CurrentSavingsAccount.create(productList[0]);
//
//            spyOn(api, '_buildAccount').andReturn(t_account);
//
//            var accountListObj = api._buildAccountList(productList, 'product');
//
//            expect(accountListObj.totalBalance).toEqual('100.12');
//            expect(api._buildAccountList).toHaveBeenCalledWith(productList[0]);
        });
    });

});